<?

class newsletterAjaxNewsletter extends ActionHandler
{
    function __construct() {
        global $user;
        global $dbLink;
        global $module;
        global $lang;

        $this->_user = $user;
        $this->_dbLink = $dbLink;
        $this->_module = $module;

        $this->_lang = $lang;

        $this->_contentClass = new newsletter(); // <--- Modificar esta linea!
        $this->_contentClass->setDBLink($dbLink);

        $this->_dbIndex = $this->_contentClass->getDBIndex();

		parent::__construct();
	}

    function delete()
    {
        if ($this->_user->isEnabledTo("write", $this->_module->id))
        {
            $arPIDs = is_array($_POST[$this->_dbIndex])? $_POST[$this->_dbIndex] : array($_POST[$this->_dbIndex]);
            foreach($arPIDs AS $objID)
            {
                if ($this->_user->getID() == (int)$objID) continue;
                if ($this->_contentClass->get($objID) && $this->_contentClass->delete()) continue;
                $response["message"] = $this->_lang->translate("error_delete_record"). mysql_error();
                break;
            }
        }
        else $response["message"] = $this->_lang->translate("user_action_denied");
        $response["success"] = $response["message"]? false : true;
        echo json_encode($response);
    }



    function newsletter_list()
    {
        if ($this->_user->isEnabledTo("read", $this->_module->id))
        {
            if ($_POST['query'])
            {
                // Useros
                $squery = new SearchQuery();
                $squery->setQueryString($_POST['query']);  // Or $queryString or whatever your string variable is
                $squery->isExact = false;     // if false, SearchQuery searchs like '%$query%', else '$query'
                $squery->table = "";
                $squery->setFields(array("email")); // Setting up search fields
                $sqlWhere = $squery->build();
            }

            $sql  = "SELECT SQL_CALC_FOUND_ROWS u.*";
            $sql .= " FROM ".$this->_contentClass->getDBTableName()." As u";
            $sql .= $sqlWhere? ' WHERE '. $sqlWhere : ' ';
            $sql .= $_POST['sort']? " ORDER BY ". txt2sql($_POST['sort'], $this->_dbLink) ." ". txt2sql($_POST['dir'], $this->_dbLink) ." " : "";
            $sql .= " LIMIT ". (int) $_POST['start'] .", ". ((int)$_POST['limit']<$this->_module->numMaxListedRows&&(int)$_POST['limit']!=0? (int)$_POST['limit'] : $this->_module->numMaxListedRows);
            $result = mysql_query($sql, $this->_dbLink);
            echo mysql_error();

            while($row = mysql_fetch_assoc($result))
            {
                $response["users"][] = array("id"       => $row['id'],                   
                                             "email"           => $row['email'],
                                             "date_created"    => dateFormat($row['date_created']));
            }
            $sql = "SELECT FOUND_ROWS()";
            list($response["totalCount"]) = mysql_fetch_row(mysql_query($sql, $this->_dbLink));
            if($response["totalCount"]==0) $response["users"] = array();
        }
        else $statusMsg = $this->_lang->translate("user_action_denied");
        echo json_encode($response);
    }
}

?>

